package com.asp.aspchain.mmwyaccount.service;

import com.alibaba.fastjson.JSON;
import com.asp.aspchain.core.contract.ERC721Full_sol_ERC721Full;
import com.asp.aspchain.core.dao.AssetMapper;
import com.asp.aspchain.core.dao.MgrContractMapper;
import com.asp.aspchain.core.dao.TocAccountMapper;
import com.asp.aspchain.core.model.AssetModel;
import com.asp.aspchain.core.model.MgrContractModel;
import com.asp.aspchain.core.model.TocAccountModel;
import com.asp.aspchain.core.util.AjaxResult;
import com.asp.aspchain.core.util.ContractUtil;
import com.asp.aspchain.mmwyaccount.constants.ErrorCode;
import com.asp.aspchain.mmwyaccount.request.QryAccountReq;
import com.asp.aspchain.mmwyaccount.response.AssetReq;
import com.asp.aspchain.mmwyaccount.response.QryBalResp;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

@Service
public class MmwyQuebalService {

	private final static Logger logger = Logger.getLogger(MmwyQuebalService.class);
	@Autowired
	private TocAccountMapper tocAccountMapper;
	@Autowired
	private MgrContractMapper mgrContractMapper;
	@Autowired
	private ContractUtil contractUtil;
	@Autowired
	private AssetMapper assetMapper;

	private String tokentype = "MMWY";

	public AjaxResult mmwyQueryBalance(String json) throws Exception {

		QryAccountReq accountReq = JSON.parseObject(json, QryAccountReq.class);
		String accountid = accountReq.getAccountid();
		MgrContractModel mgrContractModel = new MgrContractModel();
		mgrContractModel.setTokentype(tokentype);
		mgrContractModel = mgrContractMapper.selectOne(mgrContractModel);
		Integer cid = mgrContractModel.getCid();
		ERC721Full_sol_ERC721Full contract = (ERC721Full_sol_ERC721Full) contractUtil.getContract(cid);

		//检查智能合约可不可用
		boolean valid = contract.isValid();
		if (!valid) {
			return new AjaxResult(ErrorCode.SUCCESS, "mmwy智能合约不可用", "mmwy智能合约不可用");
		}

		//查询到的余额与本地的信息不一致则更新
		BigInteger balance = contract.balanceOf(accountid).send();

		TocAccountModel tocAccount = tocAccountMapper.selectById(accountid);
		BigInteger tokenvalue = tocAccount.getTokenvalue();
		if (tokenvalue.compareTo(balance) != 0) {
			TocAccountModel tocAccountModel = new TocAccountModel();
			tocAccountModel.setAccountid(accountid);
			tocAccountModel.setTokenvalue(balance);
			tocAccountMapper.updateById(tocAccountModel);
		}
		//获取tokenList
		List<BigInteger> tokenlist = contract.tokensOfOwner(accountid).send();
		// 返回响应
		List<AssetReq> assetlist = new ArrayList<>();
		for (BigInteger tokenid : tokenlist) {
			String assetid = tokenid.toString();
			AssetModel assetModel = assetMapper.selectById(assetid);
			String encodedAsset = assetModel.getAsset();
			//将资产解密获得json
			Base64.Decoder decoder = Base64.getDecoder();
			byte[] decode = decoder.decode(encodedAsset);
			String asset = "";
			try {
				asset = new String(decode, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				return new AjaxResult(ErrorCode.DECODE_ERROR, "资产解码过程出错", "资产解码过程出错");
			}
			//响应的list添加资产
			AssetReq assetReq = new AssetReq();
			assetReq.setAsset(asset);
			assetReq.setAssettype("my");
			assetlist.add(assetReq);
		}

		QryBalResp qryBalResp = new QryBalResp();
		qryBalResp.setAssetlist(assetlist);
		qryBalResp.setStatus(tocAccount.getStatus());
		qryBalResp.setTokentype(tocAccount.getTokentype());
		qryBalResp.setTokenvalue(balance);

		return new AjaxResult(ErrorCode.SUCCESS, "mmwy获取账户信息成功", qryBalResp, "mmwy获取账户信息成功");
	}

}
